package me.zingon.wxbus.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import me.zingon.wxbus.model.Line;
import me.zingon.wxbus.model.Station;
import me.zingon.wxbus.model.TimeLog;
import org.apache.tomcat.jni.Time;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class LineUtil {

    private static final Logger logger= LoggerFactory.getLogger(LineUtil.class);

    @Value("${wxbus.api.line}")
    String lineApi;

    @Value("${wxbus.api.station}")
    String stationApi;

    public List<Line> loadLine(String json, String rtSign){
        String result = this.post(json,rtSign,lineApi);
        logger.info("请求线路信息,结果：{}",result);
        JSONArray array = JSONUtil.parseObj(result).getJSONArray("list");
        return array.stream().map(line->{
            return new Line((JSONObject) line);
        }).collect(Collectors.toList());
    }

    public List<Station> loadStation(String json,String rtSign){
        String result = this.post(json,rtSign,stationApi);
        logger.info("请求站点信息,结果：{}",result);
        JSONObject resultObj = JSONUtil.parseObj(result);
        JSONObject detail = resultObj.getJSONObject("detail");
        JSONArray zxStationRunDetailInfos = detail.getJSONArray("zxStationRunDetailInfos");
        return zxStationRunDetailInfos.stream().map(station->{
            return new Station((JSONObject) station);
        }).collect(Collectors.toList());
    }

    public List<TimeLog> list(Line line){
        JSONObject json=new JSONObject();
        json.put("companyType",line.getCompanyType())
                .put("gprsId",line.getGprsId())
                .put("segmentID",line.getSegmentId());
        String result = this.post(json.toString(),line.getRtSign(),stationApi);
        JSONObject resultObj = JSONUtil.parseObj(result);
        JSONObject detail = resultObj.getJSONObject("detail");
        JSONArray zxStationRunDetailInfos = detail.getJSONArray("zxStationRunDetailInfos");

        List<TimeLog> temp=new ArrayList<>();
        zxStationRunDetailInfos.stream()
                .filter(runInfo-> ((JSONObject)runInfo).getJSONArray("zxStationBusDetailInfos").size()>0)
                .forEach(runInfo->{
                    JSONObject o= (JSONObject) runInfo;
                    o.getJSONArray("zxStationBusDetailInfos").stream()
                            .forEach(busInfo->{
                                JSONObject busInfoObj= (JSONObject) busInfo;
                                TimeLog tl=new TimeLog();
                                tl.setBusName(busInfoObj.getStr("busName"));
                                tl.setBusLng(busInfoObj.getStr("busLng"));
                                tl.setBusLat(busInfoObj.getStr("busLat"));
                                tl.setArrivalTime(busInfoObj.getDate("arrivalTime"));
                                tl.setLineId(line.getSegmentId());
                                tl.setStationId(o.getStr("stationId"));
                                tl.setCreateTime(new Date());
                                tl.setIsDeleted(0);
                                temp.add(tl);
                            });
                });
        return temp;
    }

    private String post(String json,String rtSign,String url){
        String result = HttpRequest.post(url)
                .header("rtSign",rtSign)
                .body(json)
                .execute()
                .body();
        return result;
    }
}
